import 'package:ad_plugin/ad_manager.dart';
import 'package:ad_plugin/ad_source.dart';
import 'package:ad_plugin/reward_video_ad.dart';
import 'package:ad_plugin_example/ad/banner_ad.dart';
import 'package:ad_plugin_example/ad/native_ad_test.dart';
import 'package:ad_plugin_example/ad/splash_ad_test.dart';
import 'package:ad_plugin_example/ad/video_ad.dart';
import 'package:flutter/material.dart';

import 'ad/insert_advertisement.dart';
import 'package:anythink_sdk/at_platformview/at_banner_platform_widget.dart';
import 'package:anythink_sdk/at_platformview/at_native_platform_widget.dart';
import 'package:anythink_sdk/at_native.dart';
import 'package:google_mobile_ads/google_mobile_ads.dart' as google;

import 'ad/video_ad.init.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  RewardVideoAd? rewardVideoAd;
  bool _isBannerVisible = false;
  bool _isNativeAdVisible = false;
  google.BannerAd? _bannerAd;
  google.AdWidget? _adWidget;

  @override
  void initState() {
    super.initState();
    initAdSdk();
  }

  void initAdSdk() async {
    initAdInstance();
    AdGlobalParamsManager.instance
      ..setAdAppId(adAppId: 'a632d14ba5976f')
      ..setAdAppKey(adAppKey:'3f8fcd7027f2f2d25796e8f42698ce33')
      ..setAdSource(adSource:AdSource.google);
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('AD Plugin Example App'),
        ),
        body: Center(
            child: Column(
          children: [
            ElevatedButton(
              onPressed: () {
                splashAdTest.load();
              },
              child: const Text("load splash ad"),
            ),
            ElevatedButton(
              onPressed: () {
                splashAdTest.show();
              },
              child: const Text("show splash ad"),
            ),
            ElevatedButton(
              onPressed: () {
                VideoAd().load();
              },
              child: const Text("load video ad"),
            ),
            ElevatedButton(
              onPressed: () {
                VideoAd().show();
              },
              child: const Text("play video ad"),
            ),
            ElevatedButton(
              onPressed: () {
                InsertAdvertisement().load();
              },
              child: const Text("load insert ad"),
            ),
            ElevatedButton(
              onPressed: () {
                // showInterAd();
                // interListen();
                InsertAdvertisement().show();
              },
              child: const Text("show insert ad"),
            ),
            ElevatedButton(
              onPressed: () {
                // BannerAdvertisement().load();
                google.AdSize adSize =
                    const google.AdSize(width: 300, height: 50);
                google.BannerAdListener bannerListener = google.BannerAdListener(
                  onAdLoaded: (google.Ad ad) => debugPrint("banner ad load"),
                  onAdOpened: (google.Ad ad) => debugPrint("banner ad show")
                );
                _bannerAd = google.BannerAd(
                    size: adSize,
                    adUnitId: 'ca-app-pub-3940256099942544/6300978111',
                    listener: bannerListener,
                    request: const google.AdRequest());
                _adWidget = google.AdWidget(ad: _bannerAd!);
                _bannerAd?.load();
              },
              child: const Text("load banner ad"),
            ),
            ElevatedButton(
              onPressed: () {
                // showInterAd();
                // interListen();
                setState(() {
                  _isBannerVisible = true;
                });
              },
              child: const Text("show banner ad"),
            ),
            ElevatedButton(
              onPressed: () {
                NativeAdTest().load();
              },
              child: const Text("load native ad"),
            ),
            ElevatedButton(
              onPressed: () {
                // showInterAd();
                // interListen();
                setState(() {
                  _isNativeAdVisible = true;
                });
              },
              child: const Text("show native ad"),
            ),
            Visibility(
              visible: _isBannerVisible,
              child: Container(
                alignment: Alignment.center,
                  width: _bannerAd?.size.width.toDouble(),
                  height: _bannerAd?.size.height.toDouble(),
                  child: _adWidget,
            )),
            // Visibility(
            //   visible: _isNativeAdVisible,
            //   child: Container(
            //       height: 340,
            //       color: Colors.black,
            //       child: PlatformNativeWidget(
            //         'b63414b713b112',
            //         {
            //           ATNativeManager.parent(): ATNativeManager.createNativeSubViewAttribute(
            //               340, 340,
            //               backgroundColorStr: '#FFFFFF'
            //           ),
            //           ATNativeManager.appIcon(): ATNativeManager.createNativeSubViewAttribute(
            //               50, 50,
            //               x: 10, y: 40, backgroundColorStr: 'clearColor'),
            //           ATNativeManager.mainTitle(): ATNativeManager.createNativeSubViewAttribute(
            //             340 - 190,
            //             20,
            //             x: 70,
            //             y: 40,
            //             textSize: 15,
            //           ),
            //           ATNativeManager.desc(): ATNativeManager.createNativeSubViewAttribute(
            //               340- 190, 20,
            //               x: 70, y:70, textSize: 15),
            //           ATNativeManager.cta(): ATNativeManager.createNativeSubViewAttribute(
            //               100,
            //               50,
            //               x: 340 - 110,
            //               y: 40,
            //               textSize: 15,
            //               textColorStr: "#FFFFFF",
            //               backgroundColorStr: "#2095F1"
            //           ),
            //           ATNativeManager.mainImage(): ATNativeManager.createNativeSubViewAttribute(
            //               340 - 20, 340 * 0.6,
            //               x: 10, y: 100, backgroundColorStr: '#00000000'),
            //           ATNativeManager.adLogo(): ATNativeManager.createNativeSubViewAttribute(
            //               20, 10,
            //               x: 10,
            //               y: 10,
            //               backgroundColorStr: '#00000000'),
            //           ATNativeManager.dislike(): ATNativeManager.createNativeSubViewAttribute(
            //             20,
            //             20,
            //             x: 340 - 30,
            //             y: 10,
            //           ),
            //         }
            //       ),
            // ))
          ],
        )),
      ),
    );
  }
}
